#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
//    saveFile();
//    loadFile();
//    loadHighScores();
    loadLevel();
}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){

    for(int x = 0; x < LEVEL_WIDTH; x++){
        for(int y = 0; y < LEVEL_HEIGHT; y++){
            ofRect(grid[x][y], 100, 100);
        }
    }
    
}

//--------------------------------------------------------------

void testApp::loadLevel(){
    
    ofFile level = ofFile("level.txt", ofFile::ReadOnly);
    
    string line;
    
    for(int y = 0; y < LEVEL_HEIGHT; y++){
        level >> line;
        
        for(int x = 0; x < LEVEL_WIDTH; x++){
            char c = line.at(x);
            
            if(c == 'X'){
                grid[x][y].set(x * 100, y * 100);
            } else {
                grid[x][y].set(-1000, -1000);
            }
        }
        
        ofLog(OF_LOG_ERROR, line);
    }
    
    level.close();
}

//--------------------------------------------------------------

void testApp::loadHighScores(){
    
    ofFile readFile = ofFile("scores.txt", ofFile::ReadOnly);
    
    if(readFile.is_open()){
       
        for(int i = 0; i < HIGH_SCORE_NUM; i++){
            readFile >> highScores[i];
        }
        
        readFile.close();
    } else {
        readFile.close();
        
        ofFile file = ofFile("scores.txt", ofFile::WriteOnly);
        
        for(int i = 100; i >= 10; i -= 10){
            file << i;
            file << "\n";
        }
        
        file.close();
        
        loadHighScores();
    }
}

//void testApp::saveFile(){
//    string hello = "Hello, World!";
//    
//    ofFile file = ofFile("test.txt", ofFile::WriteOnly);
//    
//    file << hello;
//    
//    file.close();
//}
//
//void testApp::loadFile(){
//    string text;
//    int highScore;
//    int highScore2;
//    
//    ofFile file = ofFile("test.txt", ofFile::ReadOnly);
//    
//    if(file.is_open()){
//        
//        for(int i = 0; i < HIGH_SCORE_NUM; i++){
//            file >> highScores[i];
//            ofLog(OF_LOG_ERROR, ofToString(highScores[i]));
//        }
//        
//        file.close();
//    } else {
//        ofFile file = ofFile("test.txt", ofFile::WriteOnly);
//        
//        for(int i = 0; i < HIGH_SCORE_NUM; i++){
//            file << 100 - i * 10;
//            file << "\n";
//        }
//        
//        file.close();
//    }
//}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}